package EGS.impl;
import java.util.List;
import java.util.concurrent.ExecutorService;

import org.dom4j.Document;
import org.dom4j.Element;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import WORKPLACE.m_resultData;
import WORKPLACE.stationClient;

public class EGS_execute_service556Impl{
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	stationClient iClient=null;
	SQLtool sql = null;
	/**
	 * 556 上传测量数据并过站
	 */
	public EGS_execute_service556Impl(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql) throws Exception {
		super();
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}

	public Document runCal() {
		String msgClass = "";
		try {
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation, iSN, iLayer,iState;

			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();

			if(msgClass.equals("556")){//上传测量数据并过站
				iSN=Mroot.element("body").element("panel").attribute("pcbID").getValue();
				iLayer=Mroot.element("body").element("panel").attribute("productSide").getValue();
				iState=Mroot.element("body").element("panel").attribute("state").getValue();
				Element eleCheckList=Mroot.element("body").element("checkList");
				List<?> checkListSons=eleCheckList.elements();
				Mroot.element("body").remove(Mroot.element("body").element("panel"));
				Element eleR=Mroot.element("body").addElement("result");
				try {
					if(checkListSons.size()>0) {
						int listLen=checkListSons.size();
						m_resultData [] iData=new m_resultData [listLen];
						for(int listI=0;listI<listLen;listI++) {
							iData[listI]=new m_resultData();
							iData[listI].pos=((Element) (checkListSons.get(listI))).attribute("pos").getValue();
							iData[listI].name=((Element)(checkListSons.get(listI))).attribute("name").getValue();
							iData[listI].unit=((Element)(checkListSons.get(listI))).attribute("unit").getValue();
							iData[listI].max=((Element)(checkListSons.get(listI))).attribute("upperLimit").getValue();
							iData[listI].min=((Element)(checkListSons.get(listI))).attribute("lowerLimit").getValue();
							iData[listI].value=((Element)(checkListSons.get(listI))).attribute("value").getValue();
							iData[listI].result=((Element)(checkListSons.get(listI))).attribute("state").getValue();
						}
						iClient.uploadData(iSN, iData);
						if(iState.equals("0")) {//556比555多个过站state，拼板的话只能整拼板一起
							iClient.uploadState(iSN, true,"-1");
						}else {
							iClient.uploadState(iSN, false,"-1");
						}
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5561,""+iSN+"_"+iStation+"_"+iLayer);
					}
				} catch (Exception e) {
					String str=errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5560,""+iSN+"_"+iStation+"_"+iLayer+"_"+str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}

}
